﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;

public partial class CanBo_PageCanBoTangLuong : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinData();
            lblCount.InnerText = "Hiện đang có " + CanBoTangLuong.All().Count() + " mục trong CSDL";
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        gvData.RowDeleting += gvCanBoTangLuong_RowDeleting;
        gvData.RowEditing += gvCanBoTangLuong_RowEditing;
        gvData.RowUpdating += gvCanBoTangLuong_RowUpdating;
        gvData.RowCancelingEdit += gvCanBoTangLuong_RowCancelingEdit;
        gvData.PageIndexChanging += gvCanBoTangLuong_PageIndexChanging;
        gvData.RowDataBound += new GridViewRowEventHandler(gvData_RowDataBound);
    }

    void gvCanBoTangLuong_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvData.PageIndex = e.NewPageIndex;
        BinData();
    }
    void BinData()
    {
        gvData.DataSource = CanBoTangLuong.All();
        gvData.DataBind();
    }
    protected void gvCanBoTangLuong_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var id = int.Parse(gvData.DataKeys[e.RowIndex]["ID"].ToString());

        CanBoTangLuong.Delete(id);
        divAlert.InnerHtml = "<b>Thông báo:</b> Xóa dữ liệu thành công." + id;
        divMsg.Visible = true;
        BinData();
    }
    protected void gvCanBoTangLuong_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvData.EditIndex = e.NewEditIndex;
        BinData();
    }
    protected void gvCanBoTangLuong_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvData.EditIndex = -1;
        BinData();
    }

    protected void gvCanBoTangLuong_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
        var id = int.Parse(gvData.DataKeys[e.RowIndex]["ID"].ToString());

        DropDownList ddlMaQTTangLuong = gvData.Rows[e.RowIndex].FindControl("ddlMaQTTangLuong") as DropDownList;
        // LoadDropDownList(ddlKhenThuong);
        var canBoTangLuong = CanBoTangLuong.Single(id);
        canBoTangLuong.MaQTTangLuong = int.Parse(ddlMaQTTangLuong.SelectedValue);
        canBoTangLuong.Bac = int.Parse(((TextBox)gvData.Rows[e.RowIndex].FindControl("txtBac")).Text);
        CanBoTangLuong.Update(canBoTangLuong);
        divAlert.InnerHtml = "<b>Thông báo:</b> Cập nhật dữ liệu thành công." + id;
        divMsg.Visible = true;
        BinData();
    }


    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        string idCollection = "";
        string strID = string.Empty;

        //Loop through GridView rows to find checked rows 
        for (int i = 0; i < gvData.Rows.Count; i++)
        {
            CheckBox chkDelete = (CheckBox)
               gvData.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                    strID = gvData.Rows[i].Cells[1].Text;
                    idCollection += strID + ",";
                }
            }
        }
        if (idCollection != "")
        {
            try
            {
                foreach (string id in idCollection.Split(','))
                    CanBoTangLuong.Delete(int.Parse(id));
            }
            catch
            {

            }
        }

        // rebind the GridView


        divAlert.InnerHtml = "<b>Thông báo:</b> Xóa dữ liệu thành công.";
        divMsg.Visible = true;

        BinData();
    }

    protected void btnRefresh_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }
    protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && gvData.EditIndex == e.Row.RowIndex)
        {
            DropDownList ddlKhenThuong = (DropDownList)e.Row.FindControl("ddlMaQTTangLuong");
            ddlKhenThuong.DataSource = QuaTrinhTangLuong.All();
            ddlKhenThuong.DataTextField = "ID";
            ddlKhenThuong.DataValueField = "PhanLoai";
            ddlKhenThuong.DataBind();
        }
    }
}